package com.kim.bookshop.order.daoinpl;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import cn.itcast.commons.CommonUtils;
import cn.itcast.jdbc.TxQueryRunner;

import com.kim.bookshop.book.bean.Book;
import com.kim.bookshop.order.bean.Order;
import com.kim.bookshop.order.bean.OrderItem;
import com.kim.bookshop.order.dao.OrderDao;

public class OrderDaoImpl implements OrderDao {

	private QueryRunner qr=new TxQueryRunner();
	
	@Override
	public void addOrder(Order order) {
		try{
			String sql="insert into orders values(?,?,?,?,?,?)";
			//处理util得Date转成sqlDate得timestamp
			Timestamp timestamp=new Timestamp(order.getOrdertime().getTime());
			
			Object[] params={order.getOid(),timestamp,
					order.getTotal(),order.getState(),order.getOwner().getUid(),order.getAddress()};
			qr.update(sql, params);
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}

	}

	@Override
	public void addOrderItemList(List<OrderItem> orderItemList) {
		try{
			String sql="insert into orderitem values(?,?,?,?,?)";
			Object[][] params=new Object[orderItemList.size()][];
			for (int i = 0; i < orderItemList.size(); i++) {
				OrderItem item=orderItemList.get(i);
				params[i]=new Object[]{item.getIid(),
									   item.getCount(),
									   item.getSubtotal(),
				                       item.getOrder().getOid(),
				                       item.getBook().getBid()};
			}
			qr.batch(sql, params);
			
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}

	}

	@Override
	public List<Order> findByUid(String uid) {
		/*
		 * 通过uid查询出当前用户下所有得List<Order>
		 * 循环遍历每个order,为其加载他所有得OrderItem
		 */
		try{
			String sql="select * from orders where uid=?";
			List<Order> orderList=qr.query(sql, new BeanListHandler<Order>(Order.class),uid);//得到当前所有订单
			for (Order order : orderList) {//循环遍历每个Order,为其加载它自己所有得订单条目
				loadOrderItems(order);
			}
			return orderList;
			
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void loadOrderItems(Order order) throws SQLException {
		String sql="select * from orderitem i, book b where i.bid=b.bid and oid=?";//查询两张表
		//一行结果集对应不再是一个javabean，所有不能用BeanListHandle
	List<Map<String,Object>> mapList=qr.query(sql,new MapListHandler(),order.getOid());
		//mapList是多个map,每个map对应一行结果集
		//  {iid:4E446BBBCCFB45B18C4253F78D925F2F,count:2,subtotal:187.20,...}
		//使用一个map生成两个对象OrderItem Book,在设置两者得关系
	List<OrderItem> orderItemList=toOrderItemList(mapList);
	order.setOrderItemList(orderItemList);

	}

	@Override
	public List<OrderItem> toOrderItemList(List<Map<String, Object>> mapList) {
		List<OrderItem> orderItemList=new ArrayList<OrderItem>();
		for (Map<String, Object> map : mapList) {
			OrderItem item=toOrderItem(map);
			orderItemList.add(item);
		}
		return orderItemList;
	}

	@Override
	public OrderItem toOrderItem(Map<String, Object> map) {
		OrderItem orderItem=CommonUtils.toBean(map, OrderItem.class);
		Book book=CommonUtils.toBean(map, Book.class);
		orderItem.setBook(book);
		return orderItem;
	}

	@Override
	public Order load(String oid) {
		try {
			String sql="select * from orders where oid=?";
			Order order= qr.query(sql, new BeanHandler<Order>(Order.class),oid);
			loadOrderItems(order); 
			return order;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public int getStateByOid(String oid) {
		try{
			String sql="select state from orders where oid=?";
			Number num=(Number)qr.query(sql, new ScalarHandler(),oid);
			return num.intValue();
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void updateState(String oid, int state) {
		try{
			String sql="update orders set state=? where oid=?";
			qr.update(sql,state,oid);
			
		}catch(SQLException e){
			throw new RuntimeException(e);
		}

	}

	@Override
	public List<Order> findAll() {
		try{
		String sql="select * from orders";
		List<Order> orderList=qr.query(sql, new BeanListHandler<Order>(Order.class));
		 for (Order order : orderList) {//循环遍历每个Order,为其加载它自己所有得订单条目
				loadOrderItems(order);
			}
			return orderList;
	}catch (SQLException e) {
		throw new RuntimeException(e);
	}
	}

	@Override
	public List<Order> unpayorder() {
		try{
			String sql="select * from orders where state=1";
			List<Order> orderList=qr.query(sql, new BeanListHandler<Order>(Order.class));
			 for (Order order : orderList) {//循环遍历每个Order,为其加载它自己所有得订单条目
					loadOrderItems(order);
				}
				return orderList;
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public List<Order> paidorder() {
		try{
			String sql="select * from orders where state=2";
			List<Order> orderList=qr.query(sql, new BeanListHandler<Order>(Order.class));
			 for (Order order : orderList) {//循环遍历每个Order,为其加载它自己所有得订单条目
					loadOrderItems(order);
				}
				return orderList;
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public List<Order> unconfirm() {
		try{
			String sql="select * from orders where state=3";
			List<Order> orderList=qr.query(sql, new BeanListHandler<Order>(Order.class));
			 for (Order order : orderList) {//循环遍历每个Order,为其加载它自己所有得订单条目
					loadOrderItems(order);
				}
				return orderList;
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public List<Order> success() {
		try{
			String sql="select * from orders where state=4";
			List<Order> orderList=qr.query(sql, new BeanListHandler<Order>(Order.class));
			 for (Order order : orderList) {//循环遍历每个Order,为其加载它自己所有得订单条目
					loadOrderItems(order);
				}
				return orderList;
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public List<Order> findByOid(String oid) {
		try{
			String sql="select * from orders where oid=?";
			List<Order> orderList=qr.query(sql, new BeanListHandler<Order>(Order.class),oid);//得到当前所有订单
			for (Order order : orderList) {//循环遍历每个Order,为其加载它自己所有得订单条目
				loadOrderItems(order);
			}
			return orderList;
			
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}



}
